# Build stage
FROM golang:1.25-alpine AS builder

# 支持多架构构建
ARG TARGETARCH
ARG APP_VERSION

# 声明镜像支持的架构
LABEL org.opencontainers.image.architectures="amd64,arm64"
# 注入作者信息
LABEL maintainer="lineCode"
# 注入版本标签
LABEL org.opencontainers.image.version="${APP_VERSION}"

# 设置工作目录
WORKDIR /app

# 优化依赖下载顺序（先下载依赖，利用缓存）
COPY go.mod go.sum ./
ENV GOPROXY=https://goproxy.cn,direct
RUN go mod download

# 复制源代码
COPY . .

# 跨平台构建（支持 M1/ARM64 和 x86_64）
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} \
    go build -a -installsuffix cgo -ldflags="-s -w" -o shorthand-go .

# Run stage
FROM alpine:latest

# 重新定义 ARG
ARG APP_VERSION

# 声明镜像支持的架构
LABEL org.opencontainers.image.architectures="amd64,arm64"
# 注入作者信息
LABEL maintainer="lineCode"
# 注入版本标签
LABEL org.opencontainers.image.version="${APP_VERSION}"

# 创建目录
RUN mkdir -p /home/shorthand
RUN mkdir -p /home/shorthand/conf
RUN mkdir -p /home/shorthand/logs

# 挂载目录
VOLUME /home/shorthand
# 指定路径
WORKDIR /home/shorthand

# 安装必要工具（如curl用于健康检查）
RUN apk --no-cache add ca-certificates curl tzdata

# 复制文件
COPY --from=builder /app/shorthand-go /home/shorthand/shorthand-go
COPY --from=builder /app/conf /home/shorthand/conf
COPY --from=builder /app/logs /home/shorthand/logs

# 暴露默认端口
EXPOSE 12800

# 使用shell形式的CMD，确保环境变量被解析
ENTRYPOINT ["/bin/sh", "-c", "./shorthand-go"]
CMD ["-mode", "dev"]
